package util;

/*
 * CollectionChangeSupport.java 28 oct. 2008
 *
 * Sweet Home 3D, Copyright (c) 2008 Emmanuel PUYBARET / eTeks <info@eteks.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

import java.util.ArrayList;
import java.util.List;

/**
 * A helper class for {@link CollectionChangeListener CollectionListener}
 * management. <code>T</code> is the type of item stored in the collection.
 * 
 * @author Emmanuel Puybaret
 */
public class CollectionChangeSupport<T> {
	private final Object source;
	private final List<CollectionChangeListener<T>> collectionListeners;

	/**
	 * Creates a collection change support.
	 */
	public CollectionChangeSupport(Object source) {
		this.source = source;
		this.collectionListeners = new ArrayList<CollectionChangeListener<T>>(5);
	}

	/**
	 * Adds the <code>listener</code> in parameter to the list of listeners that
	 * may be notified.
	 */
	public void addCollectionListener(CollectionChangeListener<T> listener) {
		this.collectionListeners.add(listener);
	}

	/**
	 * Removes the <code>listener</code> in parameter to the list of listeners
	 * that may be notified.
	 */
	public void removeCollectionListener(CollectionChangeListener<T> listener) {
		this.collectionListeners.remove(listener);
	}

	/**
	 * Fires a collection event about <code>item</code>.
	 */
	public void fireCollectionChanged(T item,
			CollectionChangeEvent.Type eventType) {
		fireCollectionChanged(item, -1, eventType);
	}

	/**
	 * Fires a collection event about <code>item</code> at a given
	 * <code>index</code>.
	 */
	@SuppressWarnings("unchecked")
	public void fireCollectionChanged(T item, int index,
			CollectionChangeEvent.Type eventType) {
		if (!this.collectionListeners.isEmpty()) {
			CollectionChangeEvent<T> furnitureEvent = new CollectionChangeEvent<T>(
					this.source, item, index, eventType);
			// Work on a copy of collectionListeners to ensure a listener
			// can modify safely listeners list
			CollectionChangeListener<T>[] listeners = this.collectionListeners
					.toArray(new CollectionChangeListener[this.collectionListeners
							.size()]);
			for (CollectionChangeListener<T> listener : listeners) {
				listener.collectionChanged(furnitureEvent);
			}
		}
	}
	
	public void removeAllCollectionListener() {
		collectionListeners.clear();
	}
}
